package studio.raptor.extractor.segment.task.assemble;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.http.client.HttpClient;
import studio.raptor.extractor.common.util.Pair;
import studio.raptor.extractor.segment.constant.EventKindConstant;
import studio.raptor.extractor.segment.constant.JsonElementName;
import studio.raptor.extractor.segment.entity.EventMeta;
import studio.raptor.hub.in.entity.ServerProxyReq;
import studio.raptor.hub.in.server.*;

/**
 * Created by liuyong8 on 2017/7/27.
 */
class NodeMsgTask implements Callable<Pair<String, String>> {
  private EventMeta eventMeta;
  private String jsonNodeName;
  private String serverUrl;
  private HttpClient httpClient;

  public NodeMsgTask(EventMeta eventMeta, String jsonNodeName, String serverUrl,HttpClient httpClient) {
    this.eventMeta = eventMeta;
    this.jsonNodeName = jsonNodeName;
    this.serverUrl = serverUrl;
    this.httpClient = httpClient;
  }

  @Override
  public Pair<String, String> call() throws Exception {
    StringBuilder sb = new StringBuilder();
    Integer kindId = eventMeta.getEventKindId();
    List<Long> params = new ArrayList<>();
    ServerProxyReq serverProxyReq = new ServerProxyReq();
    serverProxyReq.setUrl(serverUrl);

    if (JsonElementName.CUSTOMER_ORDER.equals(jsonNodeName) || JsonElementName.CUSTOMER_ORDERS.equals(jsonNodeName)) {
      if (EventKindConstant.SO_COMMIT_ORDER.equals(kindId)) {
        SoCommitOrderProxy soCommitOrderProxy = new SoCommitOrderProxy(httpClient);
        params.add(Long.valueOf(eventMeta.getObjectKey()));
        serverProxyReq.setParams(params.toArray(new Long[] {}));
        sb.append(soCommitOrderProxy.queryServerMsg(serverProxyReq)).append(",");
      } else if (EventKindConstant.SO_EXECUTION.equals(kindId)) {
        SoExecutionProxy soExecutionProxy = new SoExecutionProxy(httpClient);
        params.add(Long.valueOf(eventMeta.getObjectKey()));
        serverProxyReq.setParams(params.toArray(new Long[] {}));
        sb.append(soExecutionProxy.queryServerMsg(serverProxyReq)).append(",");
      } else if (EventKindConstant.SO_COMPLETE_ORDERITEM.equals(kindId)) {
        // 由蒿亭那边处理，不走databus
      } else if (EventKindConstant.SO_COMPLETE_ARCHIVEGROUP.equals(kindId)) {
        SoCompleteARPProxy soCompleteARPProxy = new SoCompleteARPProxy(httpClient);
        String content = eventMeta.getContent();
        List<Long> orderItemIds = JsonPath.parse(content).read("$.orderItemIds");
        params.addAll(orderItemIds);
        serverProxyReq.setParams(params.toArray(new Long[] {}));
        sb.append(soCompleteARPProxy.queryServerMsg(serverProxyReq)).append(",");
      } else if (EventKindConstant.SO_COMPLETE_ORDER.equals(kindId)) {
        SoCommitOrderProxy soCommitOrderProxy = new SoCommitOrderProxy(httpClient);
        params.add(Long.valueOf(eventMeta.getObjectKey()));
        serverProxyReq.setParams(params.toArray(new Long[] {}));
        sb.append(soCommitOrderProxy.queryServerMsg(serverProxyReq)).append(",");
      }
    } else if (JsonElementName.CUSTOMER_DETAILS.equals(jsonNodeName)) {
      if (eventMeta.getCustId() != null){
        for (Long custId : eventMeta.getCustId()) {
          CustomerProxy customerProxy = new CustomerProxy(httpClient);
          params.add(custId);
          serverProxyReq.setParams(params.toArray(new Long[] {}));
          sb.append(customerProxy.queryServerMsg(serverProxyReq)).append(",");
        }
      }else {
        sb.append(",");
      }
    } else if (JsonElementName.ACCOUNT_DETAILS.equals(jsonNodeName)) {
      if (eventMeta.getAcctId() != null){
        for (Long acctId : eventMeta.getAcctId()) {
          AccountProxy accountProxy = new AccountProxy(httpClient);
          params.add(acctId);
          serverProxyReq.setParams(params.toArray(new Long[] {}));
          sb.append(accountProxy.queryServerMsg(serverProxyReq)).append(",");
        }
      }else {
        sb.append(",");
      }
    } else if (JsonElementName.OFFER_INST_DETAILS.equals(jsonNodeName)) {
      if (eventMeta.getOfferInstId() != null){
        for (Long offerId : eventMeta.getOfferInstId()) {
          OfferInstProxy offerInstProxy = new OfferInstProxy(httpClient);
          params.add(offerId);
          params.add(eventMeta.getRegionId());
          serverProxyReq.setParams(params.toArray(new Long[] {}));
          sb.append(offerInstProxy.queryServerMsg(serverProxyReq)).append(",");
        }
      }else {
        sb.append(",");
      }
    } else if (JsonElementName.OFFER_PROD_INST_DETAILS.equals(jsonNodeName)) {
      if (eventMeta.getProdInstId() != null) {
        for (Long prodId : eventMeta.getProdInstId()) {
          ProdInstProxy prodInstProxy = new ProdInstProxy(httpClient);
          params.add(prodId);
          params.add(eventMeta.getRegionId());
          serverProxyReq.setParams(params.toArray(new Long[] {}));
          sb.append(prodInstProxy.queryServerMsg(serverProxyReq)).append(",");
        }
      }else {
        sb.append(",");
      }

    } else if (JsonElementName.EVENT_MSG.equals(jsonNodeName)) {
      sb.append(eventMeta.getContent()).append(",");
    }

    Pair<String, String> pair = new Pair<String, String>(jsonNodeName,
        sb.toString().substring(0, sb.toString().length() - 1));
    return pair;
  }


}
